package models.my;

import java.util.List;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

import models.user.UserInfo;

import play.Logger;
import play.db.ebean.Model;
import tyrex.services.UUID;
import utils.MengException;
//17
/**
 * 我的好友
 * @author zhangpeng
 *
 */

@Entity
@Table(name="my_friend")
public class MyFriend extends Model{

	private static final long serialVersionUID = 1L;
	
	@Id
	public long id;
	public String code = UUID.create();
	public String username;
	public String friend;
	public boolean status = true ;
	
	public static MyFriend getInstances(){
		return new MyFriend();
	}
	
	public static Model.Finder<Long, MyFriend> find = new Model.Finder<Long, MyFriend>(Long.class, MyFriend.class);
	
	/**
	 * 创建用户好友
	 * 
	 * @param friend
	 * @return
	 * @throws MengException
	 */
	public boolean createFriend(MyFriend friend) throws MengException {

		if (UserInfo.getInstances().verifyUsername(friend.username)) {
			throw new MengException("170001", "用户名不存在");
		}

		if (verifyMyFriend(friend.friend)) {
			throw new MengException("170002", "用户好友已存在");
		}

		try {
			friend.save();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFriend Exception," + t.getMessage());
			throw new MengException("170003", "用户好友保存失败");
		}
	}
	
	/**
	 * 用户好友移除黑名单
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean removeBlackList(String code) throws MengException {
		
		try {
			MyFriend friend = getFriend(code);
			friend.status = true;
			friend.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFriend Exception," + t.getMessage());
			throw new MengException("170003", "用户好友移除黑名单失败");
		}
	}
	
	/**
	 * 用户好友加入黑名单
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public boolean addBlackList(String code) throws MengException {
		
		try {
			MyFriend friend = getFriend(code);
			friend.status = false;
			friend.update();
			return true;
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFriend Exception," + t.getMessage());
			throw new MengException("170003", "用户好友加入黑名单失败");
		}
		
	}
	
	/**
	 * 获取用户好友列表
	 * @param username
	 * @param page
	 * @param size
	 * @return
	 * @throws MengException
	 */
	public List<MyFriend> getFriendPage(String username, int page, int size) throws MengException {
		
		try {
			return find.where().eq("username", username).findPagingList(size).getPage(page).getList();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFriend Exception," + t.getMessage());
			throw new MengException("170003", "获取用户好友列表失败");
		}
	}
	
	/**
	 * 获取用户好友
	 * @param code
	 * @return
	 * @throws MengException
	 */
	public MyFriend getFriend(String code) throws MengException {
		
		try {
			return find.where().eq("code", code).findUnique();
		} catch (Throwable t) {
			t.printStackTrace();
			Logger.error("UserInfo createFriend Exception," + t.getMessage());
			throw new MengException("170003", "获取用户好友失败");
		}
	}
	
	public boolean verifyMyFriend(String code) throws MengException {
		
		MyFriend friend = find.where().eq("code", code).findUnique();
		if(friend != null) {
			return true;
		}
		return false;
	}

}
